package mest.controller.hank;

import com.alibaba.fastjson.JSON;
import mest.entity.plan_productjob_saleout;
import mest.entity.plan_productjob_takematerial;
import mest.entity.plan_takematerial;
import mest.service.hank.PlanProductjobTakematerialService;
import mest.service.hank.PlanTakematerialService;
import mest.service.hws.BomSaveManytomanyService;
import mest.util.hankUtil.Util_Code;
import mest.util.mestUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname PlanProductjobTakematerial
 * @Description TODO
 * @Date 2019/12/5 15:18
 * @Created by 18416
 * @Auther: henk
 */

@RestController
@RequestMapping("/PlanProductjobTakematerial")
@CrossOrigin
public class PlanProductjobTakematerial {
    //本类
    @Resource
    private PlanProductjobTakematerialService pptms;
    //查询物料用的接口方法
    @Resource
    private BomSaveManytomanyService bsms;


    /**
     * 通过父级ID分页查询方法
     * @param index
     * @param pageSize
     * @param ptId
     * @return
     */
    @RequestMapping("/queryAllModel")
    public Object queryAllModel(@RequestParam Integer index,
                                @RequestParam Integer pageSize,
                                Integer ptId, HttpServletRequest request) {
        Long start = System.currentTimeMillis();
        String theirDb = mestUtil.getCompanyDB(request);
        Map<String, Object> modelMap = new HashMap<String, Object>();
        try {
            List<Map> eModel = pptms.queryAllModel_byId(index, pageSize, ptId,theirDb);

            if (!ObjectUtils.isEmpty(eModel)) {
                modelMap.put("data", eModel);
                modelMap.put("msg", "success");
                modelMap.put("code", 0);
                modelMap.put("count", pptms.findCount(ptId,theirDb));
                modelMap.put("耗时/ms:", (System.currentTimeMillis() - start));
            } else {
                modelMap.put("msg", "此对象为空值");
            }

        } catch (Exception e) {
            modelMap.put("msg", "fales");
            modelMap.put("errmsg", e.toString());
        }
        return JSON.toJSONString(modelMap);
    }



    /**
     * 1.新增
     * 2.给个接口进来果魔军用来插一插
     * @return
     */
    @RequestMapping("/createModel_PlanTakeMaterial")
    public Object createModel_PlanTakeMaterial(plan_productjob_takematerial record
            , HttpServletRequest request){
        String theirDb = mestUtil.getCompanyDB(request);
        Map<String,Object> modelMap = new HashMap<String,Object>();
        try {
            int i = pptms.insertSelective(record,theirDb);
            if (i<=0){
                modelMap.put("code", Util_Code.number_Fail );
                modelMap.put("msg",Util_Code.Fail_CODE);
            }else {
                modelMap.put("data",i);
                modelMap.put("msg",Util_Code.Success_CODE );
                modelMap.put("code",Util_Code.number_Success );
                modelMap.put("count","model");
            }

        } catch (Exception e){
            modelMap.put("code",Util_Code.number_Fail );
            modelMap.put("msg",Util_Code.Fail_CODE);
            modelMap.put("errmsg",e.toString());
        }
        return JSON.toJSONString(modelMap);
    }


    /**
     * 1.批量插入(物料清单)
     * 2.果魔君~给相应的list给我
     * @param record
     * @return
     */
    @RequestMapping("/inserList")
    public Object inserList(@RequestBody List<plan_productjob_takematerial> record
            , HttpServletRequest request ){
        String theirDb = mestUtil.getCompanyDB(request);
        int i = pptms.intsertList(record,theirDb);
        if(i>0){
        System.out.println("执行成功");
    }

        return "成功";
    }

}
